package com.ruoyi.web.controller.mes;

import java.util.List;
import java.util.Map;

import com.kt.mes.domain.StockBin;
import com.kt.mes.service.IStockBinService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.kt.mes.domain.StockBinList;
import com.kt.mes.service.IStockBinListService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 料仓详情Controller
 *
 * @author lzc
 * @date 2021-01-26
 */
@RestController
@RequestMapping("/mes/StockBinlist")
public class StockBinListController extends BaseController
{
    @Autowired
    private IStockBinListService stockBinListService;
    @Autowired
    private IStockBinService stockBinService;

    /**
     * 查询料仓详情列表
     */
  //  @PreAuthorize("@ss.hasPermi('mes:StockBinlist:list')")
    @GetMapping("/list")
    public TableDataInfo list(StockBinList stockBinList)
    {
        startPage();
        List<StockBinList> list = stockBinListService.selectStockBinListList(stockBinList);
        return getDataTable(list);
    }

    /**
     * 导出料仓详情列表
     */
   // @PreAuthorize("@ss.hasPermi('mes:StockBinlist:export')")
    @Log(title = "料仓详情", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(StockBinList stockBinList)
    {
        List<StockBinList> list = stockBinListService.selectStockBinListList(stockBinList);
        ExcelUtil<StockBinList> util = new ExcelUtil<StockBinList>(StockBinList.class);
        return util.exportExcel(list, "StockBinlist");
    }

    /**
     * 获取料仓详情详细信息
     */
  //  @PreAuthorize("@ss.hasPermi('mes:StockBinlist:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(stockBinListService.selectStockBinListById(id));
    }

    /**
     * 新增料仓详情
     */
  //  @PreAuthorize("@ss.hasPermi('mes:StockBinlist:add')")

    @PostMapping
    public AjaxResult add(@RequestBody StockBinList stockBinList)
    {

        //添加之前 确保料仓还有容量
        StockBinList query=new StockBinList();
        query.setTsbId(stockBinList.getTsbId());
        Map<String,Object> map= stockBinListService.selectStockBinSum(query);

        StockBin stockBin= stockBinService.selectStockBinById(stockBinList.getTsbId());
        //容量
        int capa=stockBin.getCapa().intValue();
        //总数
        int sum_qty;
        if(map!=null){
            sum_qty =Integer.parseInt(String.valueOf(map.get("sum_qty")));
        }else {
            sum_qty=0;
        }


        //入库数
        int rk_qty=stockBinList.getQty().intValue();


        if((rk_qty+sum_qty)>capa){

            return AjaxResult.error("料仓容量不足");
        }

        return toAjax(stockBinListService.insertStockBinList(stockBinList));
    }

    /**
     * 修改料仓详情
     */
   // @PreAuthorize("@ss.hasPermi('mes:StockBinlist:edit')")
    @Log(title = "料仓详情", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody StockBinList stockBinList)
    {
        return toAjax(stockBinListService.updateStockBinList(stockBinList));
    }

    /**
     * 删除料仓详情
     */
   // @PreAuthorize("@ss.hasPermi('mes:StockBinlist:remove')")
    @Log(title = "料仓详情", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(stockBinListService.deleteStockBinListByIds(ids));
    }

    @GetMapping("/sum")
    public AjaxResult sum(StockBinList stockBinList)
    {
        AjaxResult result=new AjaxResult();
        result.put("data",stockBinListService.selectStockBinSum(stockBinList));
        return result;
    }
}
